package com.itheima.consumer.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class MqListener {

//    @RabbitListener(queues = "simple.queue")
//    public void listenerSimpleQueue(String msg) {
//        System.out.println("消费者收到 simple.queue 的消息 ==== > " + msg);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    public void listenerWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到 work.queue 的消息 ==== > " + msg);
//        Thread.sleep(20);
//    }

//    @RabbitListener(queues = "work.queue")
//    public void listenerWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2 收到 work.queue 的消息 ++++ > " + msg);
//        Thread.sleep(200);
//    }
//
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenerFanoutQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1 收到 fanout.queue1 的消息: " + msg);
//        Thread.sleep(20);
//    }

//    @RabbitListener(queues = "fanout.queue2")
//    public void listenerFanoutQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2 收到 fanout.queue2 的消息: " + msg);
//        Thread.sleep(200);
//    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name="hmall.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenerDirectQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1 收到 direct.queue1 的消息: " + msg);
        Thread.sleep(20);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name="direct.queue2"),
            exchange = @Exchange(name="hmall.direct",type = ExchangeTypes.DIRECT),
            key={"red","yellow"}
    ))
    public void listenerDirectQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2 收到 direct.queue2 的消息: " + msg);
        Thread.sleep(200);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    @RabbitListener(queues = "topic.queue1")
    public void listenerTopicQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1 收到 topic.queue1 的消息: " + msg);
        Thread.sleep(20);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "hmall.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    @RabbitListener(queues = "topic.queue2")
    public void listenerTopicQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2 收到 topic.queue2 的消息: " + msg);
        Thread.sleep(200);
    }

    @RabbitListener(queues = "object.queue")
    public void listenSimpleQueueMessage(Map<String, Object> msg) throws InterruptedException {
        System.out.println("消费者接收到object.queue消息：【" + msg + "】");
        throw new MessageConversionException("故意的");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg){
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }
}
